package com.wish.g01.business.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wish.g01.base.entity.TbHouseInfo;
import com.wish.g01.base.entity.TbHouseStyle;
import com.wish.g01.base.entity.TbPremisesInfo;
import com.wish.g01.base.entity.TbRoomInfo;
import com.wish.g01.base.service.*;
import com.wish.g01.business.es.HouseEeRepostory;
import com.wish.g01.business.util.ThredQuery;
import com.wish.g01.business.vo.HouseInfo;
import com.wish.g01.common.core.domain.R;
import com.wish.g01.common.core.web.controller.BaseController;
import com.wish.g01.common.core.web.page.TableDataInfo;
import com.wish.g01.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.*;

/**
 * @Classname 房源控制层
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/5/28 15:03
 * @Created by wupenghao
 */
@RestController
@RequestMapping("/room")
@Slf4j
public class RoomController extends BaseController {


    @Autowired
    ITbRoomInfoService tbRoomInfoService;


    @Autowired
    RedisService redisService;

    @Autowired
    HouseEeRepostory houseEeRepostory;

    @Autowired
    ITbHouseInfoService tbHouseInfoService;

    @Autowired
    ThreadPoolTaskExecutor poolTaskExecutor;

/*    *//**
     *  es全量添加
     *//*
    @PostConstruct
    public void init() throws Exception {
      *//*  QueryWrapper<HouseInfo> houseWrapper = new QueryWrapper<>();
        // 查询房源列表
        List<TbHouseInfo> list1 = tbHouseInfoService.list();
        List<HouseInfo> list = BeanUtil.copyToList(list1, HouseInfo.class);
        // 添加到es
        houseEeRepostory.saveAll(list);*//*
        long start = System.currentTimeMillis();
        //返回结果
        List<TbHouseInfo> result = new ArrayList<>();
        // 通过count查到数据总量
        int count = tbHouseInfoService.count();

        int num = 2000;//每次查询的条数
        //需要查询的次数
        int times = count / num;
        if (count % num != 0) {
            times = times + 1;
        }
        //开始查询的行数
        int bindex = 0;
        //定义最大长度的线程池  防止线程过多
        poolTaskExecutor.setMaxPoolSize(15);
        //添加任务
        for (int i = 0; i < times; i++) {
            // 构建线程
            Callable<List<TbHouseInfo>> qfe = new ThredQuery(tbHouseInfoService, bindex, num);
            // 提交线程得到返回值
            Future<List<TbHouseInfo>> future = poolTaskExecutor.submit(qfe);
            List<TbHouseInfo> infoList = future.get();
            // 添加到集合
            if (CollectionUtils.isNotEmpty(infoList)) {
                result.addAll(infoList);
            }
            bindex = bindex + num;
        }
        // 关闭线程池
        long end = System.currentTimeMillis();
        if (log.isInfoEnabled()) {
            log.info("开启多线程查询导出数据耗时:{},共:{}条", (end - start), result.size());
        }
        //保存es
        List<HouseInfo> list = BeanUtil.copyToList(result, HouseInfo.class);
        // 添加到es
        houseEeRepostory.saveAll(list);
    }*/

    /**
     * 查询房源列表
     */
//    @PreAuthorize(hasPermi = "system:business:list")
    @GetMapping("list")
    public TableDataInfo list(TbRoomInfo tbRoomInfo)
    {
        startPage();
        List<TbRoomInfo> list=tbRoomInfoService.getList(tbRoomInfo);
        return getDataTable(list);
    }

    /**
     * 推荐房源
     */
    @PostMapping("recommend/{id}/{status}")
    public R recommend(@PathVariable("id")String id, @PathVariable("status")Integer status) {
        TbRoomInfo roomInfo = new TbRoomInfo();
        roomInfo.setId(id);
        roomInfo.setRecommended(status);
        tbRoomInfoService.saveOrUpdate(roomInfo);

        return R.ok();
    }

    /**
     * 取消推荐房源
     */
    @PostMapping("cancelRecommend/{id}/{status}")
    public R cancelRecommend(@PathVariable("id")String id,@PathVariable("status")Integer status) {
        TbRoomInfo roomInfo = new TbRoomInfo();
        roomInfo.setId(id);
        roomInfo.setRecommended(status);
        tbRoomInfoService.saveOrUpdate(roomInfo);

        return R.ok();
    }

    @Autowired
    ITbPrivateFacilityService iTbPrivateFacilityService;

    /**
     *  获取所有 私有配套信息
     *
     * @return
     */
    @GetMapping("getPrivateFacilitieTypeList")
    // @RequiresPermissions("room:roominfo:getPrivateFacilitieTypeList")
    public R getPrivateFacilitieTypeList() {
        return R.ok(iTbPrivateFacilityService.list());
    }

    @Autowired
    ITbPublicFacilityService iTbPublicFacilityService;

    /**
     *  获取所有 公共配套信息
     *
     * @return
     */
    @GetMapping("getPublicFacilitieTypeList")
    // @RequiresPermissions("room:roominfo:getPrivateFacilitieTypeList")
    public R getPublicFacilitieTypeList() {
        return R.ok(iTbPublicFacilityService.list());
    }

    @Autowired
    ISysUserService iSysUserService;

    /**
     *  获取 用户名字
     * @return
     */
    @GetMapping("getUserList")
    // @RequiresPermissions("room:roominfo:getPrivateFacilitieTypeList")
    public R getUserList() {
        return R.ok(iSysUserService.list());
    }

    @Resource
    private ITbPremisesInfoService premisesInfoService;

    /**
     *  根据名字
     * @return
     */
    @GetMapping("PremisesByName/{name}")
    // @RequiresPermissions("room:roominfo:getPrivateFacilitieTypeList")
    public R PremisesByName(@PathVariable("name") String name) {
        QueryWrapper<TbPremisesInfo> wrapper = new QueryWrapper<TbPremisesInfo>().eq("name", name);
        return R.ok(premisesInfoService.list(wrapper));
    }

    @Autowired
    ITbHouseInfoService iTbHouseInfoService;


    /**
     * 房源的添加
     * @param house
     * @return
     */
    @PostMapping("addHouse")
    public R addHouse(@RequestBody TbHouseInfo house){
       // 生成唯一主键
        String s = UUID.randomUUID().toString();
        house.setId(s);
        iTbHouseInfoService.save(house);
        // 数据增量的es
        HouseInfo houseInfo = new HouseInfo();
        BeanUtils.copyProperties(house, houseInfo);
        houseEeRepostory.save(houseInfo);
        return R.ok(house.getId());
    }
    @Autowired
    ITbHouseStyleService houseStyleService;

    /**
     * 房源的房型的添加
     * @param houseStyle
     * @return
     */
    @PostMapping("addHousestyle")
    public R addHouse(@RequestBody TbHouseStyle houseStyle){
        houseStyleService.save(houseStyle);
        return R.ok();
    }


/*    @Autowired
    HouseStyleES houseStyleES;
    *//**
     *  添加 es 列表
     * @return
     *//*
    @GetMapping("addEs")
    // @RequiresPermissions("room:roominfo:getPrivateFacilitieTypeList")
    public String addEs() {
        List<TbHouseStyle> list = houseStyleService.list();
        ArrayList<HouseStyleVo> styleVos = new ArrayList<>();
        for (TbHouseStyle tbHouseStyle : list) {
            HouseStyleVo styleVo = new HouseStyleVo();
            BeanUtils.copyProperties(tbHouseStyle,styleVo);
            styleVos.add(styleVo);
        }
        houseStyleES.saveAll(styleVos);
        return "";
    }*/


}
